package cn.classTest.mall.controller;

import cn.classTest.mall.domain.Goods;
import cn.classTest.mall.query.GoodsQueryObject;
import cn.classTest.mall.service.GoodsService;
import cn.classTest.mall.service.ShopService;
import com.github.pagehelper.PageInfo;
import cn.classTest.mall.query.QueryObject;
import cn.classTest.mall.domain.Shop;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ShopService shopService;
    /*
     * GoodsController:list
     * */
    @RequiresPermissions(value = {"goods:list", "商品列表"}, logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo) {
        PageInfo<Goods> query = goodsService.query(qo);
        List<Shop> shops = shopService.selectAll();
        model.addAttribute("pageInfo", query);
        model.addAttribute("shops", shops);
        return "goods/list";
    }
    /*
     * GoodsController:input
     * */
    @RequiresPermissions(value = {"goods:input", "商品编辑"}, logical = Logical.OR)
    @RequestMapping("/input")
    public String input(Model model, Long id, GoodsQueryObject gqo) {
        List<Shop> shops = shopService.selectAll();
        model.addAttribute("shops", shops);
        if (id != null) {
            Goods goods = goodsService.selectOne(id);
            model.addAttribute("goods", goods);
        }
        return "/goods/input";
    }
    /*
     * GoodsController:saveOrUpdate
     * */
    @RequiresPermissions(value = {"goods:saveOrUpdate", "商品新增或编辑"}, logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Goods goods) {
        goodsService.saveOrUpdate(goods);
        return "redirect:/goods/list";
    }
}
